﻿using Lucid.ESA.ClientDataSystem.Domain.Common;
using Lucid.ESA.ClientDataSystem.Domain.Entities;
using Lucid.ESA.ClientDataSystem.Domain.ParameterClasses;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Lucid.ESA.ClientDataSystem.UnitTests.Domain
{
    [TestClass]
    public class ContactTests
    {
        [TestMethod]
        public void CanAddValidContact()
        {
            Client client = Client.Create("Mauve Corporation");

            CreateContactParameters parameters = new CreateContactParameters()
            {
                Title = "Mr",
                Forename = "Barry",
                Surname = "Blue",
                EmailAddress = "barryblue@mauvecorporation.com",
                TelephoneNumber = "07777 777777",
                Client = client
            };

            Contact contact = Contact.Create(parameters);
            ValidationMessageCollection messages = contact.Validate();

            Assert.AreEqual(0, messages.Count);
            Assert.AreEqual("Mr", contact.Title);
            Assert.AreEqual("Barry", contact.Forename);
            Assert.AreEqual("Blue", contact.Surname);
            Assert.AreEqual("barryblue@mauvecorporation.com", contact.EmailAddress);
            Assert.AreEqual("07777 777777", contact.TelephoneNumber);
            Assert.AreEqual("Mr Barry Blue", contact.FullName);
            Assert.AreEqual("Mauve Corporation", contact.Client.Name);
        }

        [TestMethod]
        public void CanEditValidContact()
        {
            Client client = Client.Create("Mauve Corporation");

            CreateContactParameters createParameters = new CreateContactParameters()
            {
                Title = "Mr",
                Forename = "Barry",
                Surname = "Blue",
                EmailAddress = "barryblue@mauvecorporation.com",
                TelephoneNumber = "07777 777777",
                Client = client
            };

            Contact contact = Contact.Create(createParameters);

            EditContactParameters editParameters = new EditContactParameters()
            {
                Title = "Mr",
                Surname = "Blue",
                EmailAddress = "barry.blue@mauvecorporation.com",
                TelephoneNumber = "07777 777777"
            };

            contact.Edit(editParameters);
            ValidationMessageCollection messages = contact.Validate();

            Assert.AreEqual(0, messages.Count);
            Assert.AreEqual("Mr", contact.Title);
            Assert.AreEqual(null, contact.Forename);
            Assert.AreEqual("Blue", contact.Surname);
            Assert.AreEqual("barry.blue@mauvecorporation.com", contact.EmailAddress);
            Assert.AreEqual("07777 777777", contact.TelephoneNumber);
            Assert.AreEqual("Mr Blue", contact.FullName);
            Assert.AreEqual("Mauve Corporation", contact.Client.Name);
        }

        [TestMethod]
        public void InvalidContactReturnsErrors()
        {
            CreateContactParameters parameters = new CreateContactParameters()
            {
                Title = "Mr",
                Forename = "Barry",
                EmailAddress = "barry>blue@mauvecorp~ration.com",
                TelephoneNumber = "07#77 77~777",
            };

            Contact contact = Contact.Create(parameters);
            ValidationMessageCollection messages = contact.Validate();

            Assert.AreEqual(4, messages.Count);
            Assert.IsTrue(messages.ContainsError("Surname", "Surname cannot be empty."));
            Assert.IsTrue(messages.ContainsError("EmailAddress", "Invalid email address."));
            Assert.IsTrue(messages.ContainsError("TelephoneNumber", "Invalid telephone number."));
            Assert.IsTrue(messages.ContainsError("Client", "Client is not set."));
        }
    }
}
